package com.zbkj.admin.controller.merchant;

import com.github.pagehelper.PageInfo;
import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.model.activity.SfullDiscountActivity;
import com.zbkj.common.model.activity.SfullDiscountProduct;
import com.zbkj.common.model.merchant.MerchantAddress;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.merchant.MerchantAddressSaveRequest;
import com.zbkj.common.request.merchant.MerchantAddressSearchRequest;
import com.zbkj.common.request.merchant.MerchantSfullDiscountProductSearchRequest;
import com.zbkj.common.request.merchant.MerchantSfullDiscountSearchRequest;
import com.zbkj.common.response.SeckillActivityDetailResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.MerchantAddressManagerService;
import com.zbkj.service.service.MerchantSfullDiscountActivityService;
import com.zbkj.service.service.MerchantSfullDiscountProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * 满减折扣管理 前端控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/merchant/sfullDiscount")
@Api(tags = "商户满减折扣管理控制器")
public class MerchantSfullDiscountActivityController {

    @Autowired
    private MerchantSfullDiscountActivityService sfullDiscountActivityService;
    @Autowired
    private MerchantSfullDiscountProductService sfullDiscountProductService;

    @PreAuthorize("hasAuthority('merchant:sfullDiscount:list')")
    @ApiOperation(value = "商户满减折扣分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageInfo<SfullDiscountActivity>> getList(@Validated MerchantSfullDiscountSearchRequest request,
                                                                 @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(sfullDiscountActivityService.findPage(request, pageParamRequest));
    }

    @ApiOperation(value = "商户满减折扣详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<SfullDiscountActivity> activityDetail(@PathVariable("id") Integer id) {
        return CommonResult.success(sfullDiscountActivityService.activityDetail(id));
    }

    @PreAuthorize("hasAuthority('merchant:sfullDiscount:add')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "新增商户满减折扣")
    @ApiOperation(value = "新增商户满减折扣")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<Object> save(@RequestBody SfullDiscountActivity activity) {
        if (sfullDiscountActivityService.add(activity)) {
            //新增活动商品信息
            if (Objects.nonNull(activity.getProductList()) && activity.getProductList().size()>0)
                sfullDiscountProductService.addBatch(activity, activity.getProductList());
            return CommonResult.success().setMessage("新增商户满减折扣成功");

        }
        return CommonResult.failed().setMessage("新增商户满减折扣失败");
    }

    @PreAuthorize("hasAuthority('merchant:sfullDiscount:update')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "修改商户满减折扣")
    @ApiOperation(value = "修改商户满减折扣")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<Object> update(@RequestBody SfullDiscountActivity activity) {
        if (sfullDiscountActivityService.updateSfullDiscount(activity)) {
            //新增活动商品信息
            if (Objects.nonNull(activity.getProductList()) && activity.getProductList().size()>0)
                sfullDiscountProductService.addBatch(activity, activity.getProductList());
            return CommonResult.success().setMessage("修改商户满减折扣成功");
        }
        return CommonResult.failed().setMessage("修改商户满减折扣失败");
    }

    @PreAuthorize("hasAuthority('merchant:sfullDiscount:delete')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.DELETE, description = "删除商户满减折扣")
    @ApiOperation(value = "删除商户满减折扣")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<Object> delete(@PathVariable(value = "id") Integer id) {
        if (sfullDiscountActivityService.delete(id)) {
            return CommonResult.success().setMessage("删除商户满减折扣成功");
        }
        return CommonResult.failed().setMessage("删除商户满减折扣失败");
    }
















    @ApiOperation(value = "商户满减折扣商品分页列表")
    @RequestMapping(value = "/productList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageInfo<SfullDiscountProduct>> getproductList(@Validated MerchantSfullDiscountProductSearchRequest request,
                                                                       @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(sfullDiscountProductService.findPage(request, pageParamRequest));
    }

    @PreAuthorize("hasAuthority('merchant:sfullDiscount:delete')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.DELETE, description = "删除商户满减折扣商品")
    @ApiOperation(value = "删除商户满减折扣商品")
    @RequestMapping(value = "/deleteProduct/{ids}", method = RequestMethod.GET)
    public CommonResult<Object> delete(@PathVariable(value = "ids") Integer[] ids) {
        if (sfullDiscountProductService.delete(Arrays.asList(ids))) {
            return CommonResult.success().setMessage("删除商户满减折扣商品成功");
        }
        return CommonResult.failed().setMessage("删除商户满减折扣商品失败");
    }
}



